<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 85%;overflow-y: hidden">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form"  v-bind="layout"  :model="model" :rules="validatorRules">
        <a-form-model-item label="菜单类型" :labelCol="layout.labelCol" :wrapperCol="layout.wrapperCol" >
          <a-radio-group  v-model="model.categoryLevel" :disabled="defaultDisabled">
            <a-radio :value="1">一级分类</a-radio>
            <a-radio :value="2">二级分类</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="一级分类" :required="model.categoryLevel==2" 
          :validate-status="validateStatus"
          v-show="model.categoryLevel==2">
          <span slot="help">{{ validateStatus=='error'?'请选择上级菜单':'&nbsp;&nbsp;' }}</span>
          <a-select v-model="model.parentId" :disabled="defaultDisabled"  placeholder="请选择一级分类">
              <a-select-option v-for="obj in parentData" :key="obj.value" :value="obj.value">{{ obj.text }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="分类名称" required prop="categoryName">
          <a-input v-model="model.categoryName" placeholder="请输入分类名称"/>
        </a-form-model-item>
        <a-form-model-item label="分类排序" required prop="categoryOrder">
          <a-input-number  v-model="model.categoryOrder"  placeholder="请输入分类排序" style="width: 100%" />
        </a-form-model-item>
        <a-form-model-item label="是否启用">
          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="model.categoryStatus"/>
        </a-form-model-item>
        <a-form-model-item label="图标" >
          <j-image-upload class="avatar-uploader" text="上传" v-model="model.iconImg" :isMultiple="false"></j-image-upload>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>

  import { postAction,getAction } from '@/api/manage'

  export default {
    name: "CategoryModel",
    data () {
      return {
        title:"操作",
        visible: false,
        defaultDisabled: false,
        model: {categoryLevel:1,categoryStatus:true},
        show:false,
        layout: {
          labelCol: { span: 3 },
          wrapperCol: { span: 14 },
        },
        confirmLoading: false,
        validatorRules:{
          categoryName: [
            { required: true, message: '请输入分类名称!' },
            { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
          ],
          categoryOrder: [
            { required: true, message: '请输入分类排序!'}
          ]
        },
        parentData:[],
        validateStatus:"",
        url:{
          add:"/adh/shopCategory/add",
          edit:"/adh/shopCategory/edit",
          parentData:"/adh/shopCategory/queryParentData"
        }
      }
    },
    created () {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      var that = this;
    },
    methods: {
      add () {
        this.queryParentData();
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.queryParentData();
        this.model = Object.assign({}, record);
        this.visible = true;
        //编辑页面禁止修改角色编码
        if(this.model.id){
          this.defaultDisabled = true;
        }else{
          this.defaultDisabled = false;
        }
      },
      close () {
        this.$refs.form.clearValidate();
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            if(this.model.categoryLevel==2 && !this.model.parentId){
              that.validateStatus = 'error';
              return;
            } else {
              that.validateStatus = 'success';
            }
            that.confirmLoading = true;
            let url = "";
            if(!this.model.id){
              url = that.url.add;
            }else{
              url = that.url.edit;
            }
            postAction(url,this.model).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(()=>{
              that.confirmLoading = false;
              that.close();
            })
          }else{
            return false;
          }
        })
      },
      handleCancel () {
        this.close()
      },
      queryParentData(){
        var that = this;
        getAction(this.url.parentData,{}).then((res)=>{
        if(res.success){
            that.parentData = res.result;
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>